<template>
  <el-dialog
    title="去结算"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">

      <el-form-item label="收货单位" prop="companyId">
        <el-select style="width: 100%" :disabled="!dataForm.id ? false : true" v-model="dataForm.companyId"
                   placeholder="收货单位">
          <el-option
            v-for="item in receiveCompanyList"
            :key="item.id"
            :label="item.name"
            :value="+item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出票人" prop="ticketMaker">
        <el-input v-model="dataForm.ticketMaker" placeholder="出票人"></el-input>
      </el-form-item>
      <el-form-item label="收票人" prop="ticketCollector">
        <el-input v-model="dataForm.ticketCollector" placeholder="收票人"></el-input>
      </el-form-item>

      <el-form-item label="出票日期" prop="ticketDate">
        <el-date-picker style="width: 100%"
                        type="date"
                        v-model="dataForm.ticketDate"
                        value-format="yyyy-MM-dd"
                        placeholder="出票日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="">
        <el-input type="textarea" v-model="dataForm.note" placeholder="备注"></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">结算</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Utils from '@/utils/utils'

export default {
  props: ['receiveCompanyList'],
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        ticketDate: '',
        companyId: '',
        ticketMaker: '',
        ticketCollector: '',
        note: ''
      },
      dataRule: {
        companyId: [
          {required: true, message: '收货单位', trigger: 'blur'}
        ],
        ticketDate: [
          {required: true, message: '出票日期', trigger: 'blur'}
        ],
        ticketMaker: [
          {required: true, message: '出票人', trigger: 'blur'}
        ],
        ticketCollector: [
          {required: true, message: '收票人', trigger: 'blur'}
        ],
        note: [
          {required: false, message: '备注', trigger: 'blur'}
        ]
      }
    }
  },
  mounted() {
    this.dataForm.ticketDate = Utils.getCurrentDate();
  },
  methods: {

    init(id, companyId) {
      this.dataForm.id = id || 0
      this.dataForm.companyId = companyId;
      this.visible = true
      this.$refs['dataForm'].resetFields()

    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/tms/settlementrecord/settle`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id,
              'ticketCollector': this.dataForm.ticketCollector,
              'ticketDate': this.dataForm.ticketDate,
              'ticketMaker': this.dataForm.ticketMaker,
              'note': this.dataForm.note

            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
